package com.wyc.price.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyc.internalCommon.constant.CommonStatusEnum;
import com.wyc.internalCommon.request.PriceRuleDTO;
import com.wyc.internalCommon.result.ApiResult;
import com.wyc.price.mapper.PriceRuleMapper;
import com.wyc.price.service.PriceRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 运价规则Service业务层处理
 * 
 * @author li
 * @date 2023-03-23
 */
@Service
public class PriceRuleServiceImpl implements PriceRuleService
{
    @Autowired
    private PriceRuleMapper priceRuleMapper;


    /**
     * 查询运价规则列表
     * 
     * @param priceRuleDTO 运价规则
     * @return 运价规则
     */
    @Override
    public List<PriceRuleDTO> selectPriceRuleList(PriceRuleDTO priceRuleDTO)
    {
        return priceRuleMapper.selectPriceRuleList(priceRuleDTO);
    }

    /**
     * 新增运价规则
     * 
     * @param priceRuleDTO 运价规则
     * @return 结果
     */
    @Override
    public ApiResult insertPriceRule(PriceRuleDTO priceRuleDTO)
    {
        String cityCode = priceRuleDTO.getCityCode();
        String vehicleType = priceRuleDTO.getVehicleType();
        // 运价类型
        String fareType = cityCode+"$"+vehicleType;
        priceRuleDTO.setFareType(fareType);
        int fareVersion = 0;

        QueryWrapper<PriceRuleDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code",cityCode)
                .eq("vehicle_type",vehicleType)
                        .orderByDesc("fare_version");
        List<PriceRuleDTO> list = priceRuleMapper.selectList(queryWrapper);
        if (list.size() > 0){
            return ApiResult.error(CommonStatusEnum.PRICE_RULE_EXISTS.getCode(),CommonStatusEnum.PRICE_RULE_EXISTS.getMsg());
        }

        priceRuleDTO.setFareVersion(++fareVersion);
        int i = priceRuleMapper.insertPriceRule(priceRuleDTO);
        return ApiResult.success(i);
    }

    /**
     * 修改运价规则
     * 
     * @param priceRuleDTO 运价规则
     * @return 结果
     */
    @Override
    public ApiResult updatePriceRule(PriceRuleDTO priceRuleDTO)
    {
        String cityCode = priceRuleDTO.getCityCode();
        String vehicleType = priceRuleDTO.getVehicleType();
        // 运价类型
        String fareType = cityCode+"$"+vehicleType;
        priceRuleDTO.setFareType(fareType);
        int fareVersion = 0;

        QueryWrapper<PriceRuleDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code",cityCode)
                .eq("vehicle_type",vehicleType)
                .orderByDesc("fare_version");
        List<PriceRuleDTO> list = priceRuleMapper.selectList(queryWrapper);
        if (list.size() == 0){
            // 运价规则不存在
            return ApiResult.error(CommonStatusEnum.PRICE_RULE_NOT_EXISTS.getCode(),CommonStatusEnum.PRICE_RULE_NOT_EXISTS.getMsg());
        }
        // 拿到运价规则最新版本号，更新在添加一条规则，版本号变更，保存原有数据
        PriceRuleDTO priceRule = list.get(0);
        if (priceRule.getStartFare().doubleValue() == priceRuleDTO.getStartFare().doubleValue()
        && priceRule.getStartMile().doubleValue() == priceRuleDTO.getStartMile().doubleValue()
        && priceRule.getUnitPricePerMile().doubleValue() == priceRuleDTO.getUnitPricePerMile().doubleValue()
        && priceRule.getUnitPricePerMinute().doubleValue() == priceRuleDTO.getUnitPricePerMinute().doubleValue()){
            return ApiResult.error(CommonStatusEnum.PRICE_RULE_EXISTS.getCode(),CommonStatusEnum.PRICE_RULE_EXISTS.getMsg());
        }
        // 迭代版本号
        fareVersion = priceRule.getFareVersion();
        priceRuleDTO.setFareVersion(++fareVersion);
        int i = priceRuleMapper.insertPriceRule(priceRuleDTO);
        return ApiResult.success(i);
    }

    /**
     * 判断该城市和对应车型的计价规则是否存在
     * @param priceRuleDTO
     * @return
     */
    @Override
    public ApiResult ifExists(PriceRuleDTO priceRuleDTO) {
        QueryWrapper<PriceRuleDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code",priceRuleDTO.getCityCode())
                    .eq("vehicle_type",priceRuleDTO.getVehicleType())
                    .orderByDesc("fare_version");
        Integer integer = priceRuleMapper.selectCount(queryWrapper);
        boolean flag = true;
        if (integer == 0){
            flag = false;
        }
        return ApiResult.success(flag);
    }

    /**
     * 计算运价规则是否是最新
     * @param priceRuleDTO
     * @return
     */
    @Override
    public ApiResult isNew(PriceRuleDTO priceRuleDTO) {
        QueryWrapper<PriceRuleDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code",priceRuleDTO.getCityCode())
                    .eq("vehicle_type",priceRuleDTO.getVehicleType())
                    .eq("fare_type",priceRuleDTO.getFareType())
                    .orderByDesc("fare_version");
        List<PriceRuleDTO> list = priceRuleMapper.selectList(queryWrapper);
        PriceRuleDTO priceRule = list.get(0);
        boolean flag = true;
        if (!priceRuleDTO.getFareVersion().equals(priceRule.getFareVersion())){
            flag = false;
        }
        return ApiResult.success(flag);
    }

}
